﻿Namespace News

    Public Class ArchiveController
        Inherits AzoxController

        '
        ' GET: /news/archive

        Function Index(Optional pageIndex As Integer = 0) As ActionResult
            Dim pageSize As Double = My.Settings.PageSize
            Dim totalRecords As Double = Db.News.Count

            ViewBag.Title = "Новости"
            ViewBag.PageIndex = pageIndex
            ViewBag.PageCount = CInt(Math.Ceiling(totalRecords / pageSize))
            ViewBag.CreateUrl = Url.Action("create", "admin", New With {.area = "news"})

            ViewBag.YearList = Db.News.Where(Function(m) Not m.Draft).GroupBy(Function(m) m.PublishDate.Year).OrderByDescending(Function(m) m.Key).Select(Function(m) m.Key).ToList
            ViewBag.Message = "Извините, новостей нет."

            Return View(Db.News.Where(Function(m) Not m.Draft).OrderByDescending(Function(m) m.PublishDate).Skip(pageIndex * CInt(pageSize)).Take(CInt(pageSize)).ToList)
        End Function

        '
        ' GET: /news/archive/details/*

        Function Details(Optional id As Guid = Nothing) As ActionResult
            Dim model As NewsItem = Db.News.Find(id)
            If IsNothing(model) Then
                Return HttpNotFound()
            End If

            ViewBag.Title = model.Title
            ViewBag.EditUrl = Url.Action("edit", "admin", New With {.id = model.Id, .area = "news", .returnUrl = Url.Action("details", New With {.id = model.Id})})
            ViewBag.ParentUrl = "/news"

            Return View(model)
        End Function

        '
        ' GET: /news/archive/year/*

        Function Year(id As Integer) As ActionResult
            ViewBag.Title = String.Format("Архив новостей за {0} год", id)
            ViewBag.ParentUrl = "/news"

            ViewBag.YearList = Db.News.Where(Function(m) Not m.Draft).GroupBy(Function(m) m.PublishDate.Year).OrderByDescending(Function(m) m.Key).Select(Function(m) m.Key).ToList
            ViewBag.Message = String.Format("К сожалению, архив не содержит новостей за {0} год.", id)

            Return View(Db.News.Where(Function(m) Not m.Draft And m.PublishDate.Year = id).OrderByDescending(Function(m) m.PublishDate).ToList)
        End Function

    End Class

End Namespace
